import axios from 'axios';

// 音乐服务API基础URL - 使用NeteaseCloudMusicApi
const API_BASE_URL = 'https://netease-cloud-music-api-alpha.vercel.app';

// 歌曲类型定义
export interface Song {
  id: string;
  title: string;
  artist: string;
  cover: string;
  src: string;
}

/**
 * 搜索音乐
 * @param keywords 搜索关键词
 * @param limit 返回结果数量限制
 * @returns 歌曲列表
 */
export async function searchMusic(keywords: string, limit: number = 10): Promise<Song[]> {
  try {
    const response = await axios.get(`${API_BASE_URL}/search`, {
      params: {
        keywords,
        limit
      }
    });

    if (response.data.code === 200 && response.data.result && response.data.result.songs) {
      return response.data.result.songs.map((song: any) => ({
        id: song.id.toString(),
        title: song.name,
        artist: song.artists.map((artist: any) => artist.name).join(', '),
        cover: `https://p2.music.126.net/${song.album.picUrl.split('/').pop()}/${song.album.pic}.jpg`,
        src: `https://music.163.com/song/media/outer/url?id=${song.id}.mp3`
      }));
    }
    
    return [];
  } catch (error) {
    console.error('搜索音乐失败:', error);
    return [];
  }
}

/**
 * 获取热门歌曲
 * @param limit 返回结果数量限制
 * @returns 热门歌曲列表
 */
export async function getHotSongs(limit: number = 10): Promise<Song[]> {
  try {
    // 这里使用热歌榜API
    const response = await axios.get(`${API_BASE_URL}/playlist/detail`, {
      params: {
        id: '3778678' // 热歌榜ID
      }
    });

    if (response.data.code === 200 && response.data.playlist && response.data.playlist.tracks) {
      return response.data.playlist.tracks.slice(0, limit).map((song: any) => ({
        id: song.id.toString(),
        title: song.name,
        artist: song.ar.map((artist: any) => artist.name).join(', '),
        cover: song.al.picUrl || 'https://p2.music.126.net/6y-UleORITEDbvrOLV0Q8A==/5639395138885805.jpg',
        src: `https://music.163.com/song/media/outer/url?id=${song.id}.mp3`
      }));
    }
    
    return [];
  } catch (error) {
    console.error('获取热门歌曲失败:', error);
    return [];
  }
}

/**
 * 获取推荐歌曲
 * @param limit 返回结果数量限制
 * @returns 推荐歌曲列表
 */
export async function getRecommendSongs(limit: number = 10): Promise<Song[]> {
  try {
    const response = await axios.get(`${API_BASE_URL}/personalized/newsong`, {
      params: {
        limit
      }
    });

    if (response.data.code === 200 && response.data.result) {
      return response.data.result.map((item: any) => {
        const song = item.song;
        return {
          id: song.id.toString(),
          title: song.name,
          artist: song.artists.map((artist: any) => artist.name).join(', '),
          cover: song.album.picUrl || 'https://p2.music.126.net/6y-UleORITEDbvrOLV0Q8A==/5639395138885805.jpg',
          src: `https://music.163.com/song/media/outer/url?id=${song.id}.mp3`
        };
      });
    }
    
    return [];
  } catch (error) {
    console.error('获取推荐歌曲失败:', error);
    return [];
  }
}

/**
 * 获取歌曲详情
 * @param id 歌曲ID
 * @returns 歌曲详情
 */
export async function getSongDetail(id: string): Promise<Song | null> {
  try {
    const response = await axios.get(`${API_BASE_URL}/song/detail`, {
      params: {
        ids: id
      }
    });

    if (response.data.code === 200 && response.data.songs && response.data.songs.length > 0) {
      const song = response.data.songs[0];
      return {
        id: song.id.toString(),
        title: song.name,
        artist: song.ar.map((artist: any) => artist.name).join(', '),
        cover: song.al.picUrl || 'https://p2.music.126.net/6y-UleORITEDbvrOLV0Q8A==/5639395138885805.jpg',
        src: `https://music.163.com/song/media/outer/url?id=${song.id}.mp3`
      };
    }
    
    return null;
  } catch (error) {
    console.error('获取歌曲详情失败:', error);
    return null;
  }
} 